package com.itheima01.table02;

import com.itheima01.bean.Classes;
import com.itheima01.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Test02 {
    @Test
    public void findAll() throws IOException {
        InputStream is = Resources.getResourceAsStream("MyConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        OneToManyMapper mapper = sqlSession.getMapper(OneToManyMapper.class);
        List<Classes> classes = mapper.findAll();
        for (Classes clas :classes){
            System.out.println(clas.getId()+","+clas.getName());
            List<Student> students = clas.getStudents();
            for (Student student : students) {
                System.out.println("\t"+student);
            }
        }
        sqlSession.close();
        is.close();
    }
}
